package demo.lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Arnold Yand
 * @since Mar 10, 20172:58:48 PM
 * @summary demo.lock.AtomicClassDemo.java
 */
public class AtomicClassDemo {

    public static void main(String[] args) {
        final AtomicInteger count = new AtomicInteger(0);
        System.out.println("init value for count is " + count.get() + ", and expect value is 100");
        ExecutorService executor = Executors.newFixedThreadPool(8);
        for (int i = 0; i < 100; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    count.getAndAdd(1);
                }
            });
        }
        try {
            executor.shutdown();
            executor.awaitTermination(60, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
        }
        System.out.println("curent value for count is " + count.get());

    }

}
